﻿<%@ WebHandler Language="C#" Class="DisplaySmallPic" %>

using System;
using System.Web;
using Microsoft.VisualBasic.Devices;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;

public class DisplaySmallPic : IHttpHandler
{


    public void ProcessRequest(HttpContext context)
    {
        // 取得传递进来的文件路径。
        string PictureFilePath = context.Request.QueryString["PictureFilePath"] + "";

        if (System.IO.File.Exists(PictureFilePath))
        {

            System.Drawing.Image image = System.Drawing.Image.FromFile(PictureFilePath);
            double rate = (Convert.ToDouble(image.Width) / Convert.ToDouble(image.Height));

            string picwidth = context.Request.QueryString["picwidth"] + "";
            string picheight = context.Request.QueryString["picheight"] + "";
            int picwidth1 = 0;
            int picheight1 = 0;



            //宽有,高缺省
            if ((picwidth != "" && functions.IsInteger(picwidth)) && (picheight == "" || !functions.IsInteger(picheight)))
            {
                picwidth1 = Convert.ToInt32(picwidth);
                picheight1 = Convert.ToInt32(picwidth1 / rate);

            }
            //宽缺省，高有

            else if ((picwidth == "" || !functions.IsInteger(picwidth)) && (picheight != "" && functions.IsInteger(picheight)))
            {
                picheight1 = Convert.ToInt32(picheight);
                picwidth1 = Convert.ToInt32(picheight1 * rate);


            }
            //宽高都缺省
            else if ((picwidth == "" || !functions.IsInteger(picwidth)) && (picheight == "" || !functions.IsInteger(picheight)))
            {
                picheight1 = image.Height;
                picwidth1 = image.Width;


            }
            //宽高都有
            else if ((picwidth != "" && functions.IsInteger(picwidth)) && (picheight != "" && functions.IsInteger(picheight)))
            {
                picheight1 = Convert.ToInt32(picheight);
                picwidth1 = Convert.ToInt32(picwidth);


            }
            else
            {
                picheight1 = image.Height;
                picwidth1 = image.Width;

            }

            //if (picwidth == "" || !functions.IsInteger(picwidth))
            //{
            //    picwidth1 = 160;
            //}
            //else
            //{
            //    picwidth1 = Convert.ToInt32(picwidth);
            //}
            //if (picwidth == "" || !functions.IsInteger(picwidth))
            //{
            //    picheight1 = 160;
            //}
            //else
            //{
            //    picheight1 = Convert.ToInt32(picheight);
            //}







            if (PictureFilePath.Length > 0) PictureFilePath = PictureFilePath.Trim();

            // 改变 HTTP 文件头的输出格式，以便让浏览器知道所输出的文件格式是 JPEG 图文件。
            context.Response.ContentType = "Image/JPEG";
            context.Response.Clear();
            context.Response.BufferOutput = true;

            Computer MyComputer = new Computer();

            try
            {
                using (MemoryStream ms = new MemoryStream(MyComputer.FileSystem.ReadAllBytes(PictureFilePath)))
                {
                    using (Bitmap bmp = new Bitmap(ms))
                    {
                        // 将照片缩小。
                        using (Bitmap SmallerBmp = new Bitmap(bmp, picwidth1, picheight1))
                        {
                            SmallerBmp.Save(context.Response.OutputStream, ImageFormat.Jpeg);
                        }
                    }
                }
            }
            catch
            {

            }
            finally
            {
                image.Dispose();
            }
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}